package com.lkc.service.impl;

import com.lkc.annotation.LogMethod;
import com.lkc.bean.User;
import com.lkc.mapper.UserDao;
import com.lkc.service.UserService;
import com.lkc.util.Page;
import com.lkc.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @LogMethod(serviceName = "普通用户服务", recordParam = true, recordReturn = true)
    public User add(User user) {
        // 对密码进行加密处理
        if (user.getPassword() != null && !user.getPassword().isEmpty()) {
            user.setPassword(PasswordUtil.getEncryption(user.getPassword()));
        }
        userDao.add(user);
        return userDao.one(user.getUserId());
    }

    @Override
    @LogMethod(serviceName = "普通用户服务", recordParam = true, recordReturn = true)
    public int del(Integer userId) {
        return userDao.del(userId);
    }

    @Override
    @LogMethod(serviceName = "普通用户服务", recordParam = true, recordReturn = true)
    public User edit(User user) {
        // 如果密码不为空，则进行加密处理
        if (user.getPassword() != null && !user.getPassword().isEmpty()) {
            user.setPassword(PasswordUtil.getEncryption(user.getPassword()));
        } else {
            // 如果密码为空，不更新密码字段
            user.setPassword(null);
        }
        userDao.edit(user);
        return userDao.one(user.getUserId());
    }

    @Override
    @LogMethod(serviceName = "普通用户服务", recordParam = true, recordReturn = true)
    public User one(Integer userId) {
        return userDao.one(userId);
    }

    @Override
    @LogMethod(serviceName = "普通用户服务", recordParam = true, recordReturn = false)
    public List<User> all(User user) {
        return userDao.all(user);
    }

    @Override
    @LogMethod(serviceName = "普通用户服务", recordParam = true, recordReturn = false)
    public void page(Page page, User user) {
        int count = userDao.pageCount(user);
        Object data = userDao.page(page, user);
        page.setCount(count);
        page.setData(data);
    }

    @Override
    @LogMethod(serviceName = "普通用户服务", recordParam = true, recordReturn = true)
    public User getByUsername(String username) {
        return userDao.getByUsername(username);
    }
}
